/**
 * 前3排行榜
 */
class TopThreeView extends egret.Sprite{


    private contentView:egret.Sprite = null;
    private topData:any = null;

    constructor(data:any){
        super();
        this.topData = data;
        if(!Array.isArray(this.topData) || this.topData.length <=0){
            Toast.makeText({text:'排行榜数据异常！'}).show();
            this.removeSelf();
            return;
        }
        this.initView();
        this.touchEnabled = true;
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.addToStage,this);
    }

    private addToStage():void{
        this.addAnimation();
    }


    private initView():void{
        this.width = GameData.stageW;
        this.height = GameData.stageH;
        //遮罩
        this.graphics.beginFill(0x000000,0.7);
        this.graphics.drawRect(0,0,this.width,this.height);
        this.graphics.endFill();

        //背景
        let bg = utils.createBitmapByName('ph_bg');
        let box = utils.createSprite({
            width:bg.width,
            height:bg.height,
            offsetX:0.5,
            offsetY:0.5,
            x:this.width/2,
            y:this.height/2
        });
        this.contentView = box;
        this.addChild(box);
        box.addChild(bg);

        var len = this.topData.length;
        if(len > 0){
            var top1 = new TopItem(this.topData[0]);
            top1.x = box.width/2;
            top1.y = box.height/2 + 50;
            box.addChild(top1);
            var f = utils.createBitmapByName('icon_1',{offsetX:0.5,offsetY:0.5});
            f.x = top1.x - top1.width/2 + 10;
            f.y = top1.y - top1.height/2 + 10;
            box.addChild(f);
        }

        if(len > 1){
            var top2 = new TopItem(this.topData[1]);
            top2.scaleX = top2.scaleY = 0.88;
            top2.x = 150;
            top2.y = 530;
            box.addChild(top2);
            var s = utils.createBitmapByName('icon_2',{offsetX:0.5,offsetY:0.5});
            s.x = top2.x - top2.width/2*0.88 + 10;
            s.y = top2.y - top2.height/2*0.88 + 10;
            box.addChild(s);
        }

        if(len > 2){
            var top3 = new TopItem(this.topData[2]);
            top3.scaleX = top3.scaleY = 0.8;
            top3.x = 580;
            top3.y = 580;
            box.addChild(top3);
            var t = utils.createBitmapByName('icon_3',{offsetX:0.5,offsetY:0.5});
            t.x = top3.x - top3.width/2*0.8 + 10;
            t.y = top3.y - top3.height/2*0.8 + 10;
            box.addChild(t);
        }



        let btn = utils.createBitmapByName('x',{
            offsetX:0.5,
            offsetY:0.5
        });
        btn.touchAnimation = true;
        btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.removeSelf,this);
        btn.x = box.width - btn.width/2;
        btn.y = 130;
        box.addChild(btn);
    }


    private removeSelf():void{
        Main.removeTop3View();
    }

    // 添加动画
    private addAnimation(){
        this.contentView.scaleX =  0.1;
        this.contentView.scaleY =  0.1;
        egret.Tween.removeTweens(this.contentView);
        egret.Tween.get(this.contentView).to({scaleX:1, scaleY:1}, 500, egret.Ease.backOut);
    }
}